import axios from "axios";

import Cookies from "js-cookie";

const instance = axios.create({
  baseURL: "http://120.24.64.5:8088/mall-admin",
  timeout: 5000,
});

// 拦截器

instance.interceptors.request.use(
  (config) => {
    // 设置请求头
    const token = Cookies.get("token");
    // 覆盖操作
    // if (token) {
    //   config.headers = {
    //     Authorization: token,
    //   };
    //   // 添加操作
    //   config.headers!["Authorization"] = token;
    // }

    // 短路运算
    token && (config.headers!["Authorization"] = token);
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

instance.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default instance;
